package com.richie696.rabbit;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.richie696.rabbit.commons.RabbitKey.*;

/**
 * 消息生产者配置类
 *
 * @author richie696
 * @version 1.0
 * @since 2021/04/25
 */
@Configuration
public class ApplicationConfig {

    @Bean("exchangeOrder")
    public Exchange exchangeOrder() {
        return ExchangeBuilder.directExchange(ORDER_EXCHANGE_TTL).durable(true).build();
    }

    @Bean("queueOrder")
    public Queue queueOrder() {
        Map<String, Object> arguments = new HashMap<>(5);
        arguments.put("x-message-ttl", ORDER_TTL_TIME);
        arguments.put("x-dead-letter-exchange", ORDER_EXCHANGE_DLX);
        arguments.put("x-dead-letter-routing-key", ORDER_KEY_DLX);
        return QueueBuilder.durable(ORDER_QUEUE_TTL).deliveryLimit(2).withArguments(arguments).build();
    }

    @Bean
    public Binding bindingOrder(Exchange exchangeOrder, Queue queueOrder) {
        return BindingBuilder.bind(queueOrder).to(exchangeOrder).with(ORDER_KEY_TTL).noargs();
    }

    @Bean("exchangeOrderDlx")
    public Exchange exchangeOrderDlx() {
        return ExchangeBuilder.directExchange(ORDER_EXCHANGE_DLX).build();
    }

    @Bean("queueOrderDlx")
    public Queue queueOrderDlx() {
        return QueueBuilder.durable(ORDER_QUEUE_DLX).build();
    }

    @Bean
    public Binding bindingOrderDlx(Exchange exchangeOrderDlx, Queue queueOrderDlx) {
        return BindingBuilder.bind(queueOrderDlx).to(exchangeOrderDlx).with(ORDER_KEY_DLX).noargs();
    }

}
